#!/usr/bin/env python

import wsgiref.handlers
from google.appengine.ext import db
from google.appengine.ext import webapp
from schema import RankComputation
import logging

class IterationIncrementer(webapp.RequestHandler):
  def post(self):

    computationid = long(self.request.get("computationid"))
    logging.debug("This is on behalf of computation %s." % computationid)
    computation = RankComputation.get_by_id(computationid)
    computation.iteration_count += 1
    computation.put()

    # Begin another iteration
    body = '<form action="/rank-nodes" method="POST">'
    body += '<input type="hidden" name="computationid" value="'
    body += str(computationid) + '" />'
    body += '<input type="submit" value="Begin Again" /></form>'
    self.response.out.write("<html><body>%s</body></html>" % body)

def main():
  wsgiref.handlers.CGIHandler().run(webapp.WSGIApplication([
    ('/increment-iteration', IterationIncrementer),
  ]))

if __name__ == '__main__':
  main()
